package com.enn.demo.core.service.impl.infrastructure.repository;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.enn.demo.core.service.impl.domain.enums.DelFlagEnum;
import com.enn.demo.core.service.impl.domain.model.person.Person;
import com.enn.demo.core.service.impl.domain.model.person.PersonId;
import com.enn.demo.core.service.impl.domain.model.person.PersonRepository;
import com.enn.demo.core.service.impl.infrastructure.converter.PersonConverter;
import com.enn.demo.dal.entity.SysPersonDO;
import com.enn.demo.dal.mapper.SysPersonMapper;
import org.springframework.stereotype.Repository;

/**
 * @author kll
 * @date 2021/5/14
 */
@Repository
public class PersonRepositoryImpl extends ServiceImpl<SysPersonMapper, SysPersonDO> implements PersonRepository, IService<SysPersonDO> {


    @Override
    public Person find(PersonId personId) {
        SysPersonDO sysPersonDO = this.getById(personId.getId());
        return PersonConverter.toPerson(sysPersonDO);
    }

    @Override
    public void remove(PersonId personId) {
        SysPersonDO sysPersonDO = this.getById(personId.getId());
        sysPersonDO.setDelFlag(DelFlagEnum.DELETED.getValue());
        this.removeById(sysPersonDO);
    }

    @Override
    public PersonId store(Person person) {
        //保存或更新
        SysPersonDO sysPersonDO = PersonConverter.fromPerson(person);
        this.saveOrUpdate(sysPersonDO);
        return new PersonId(sysPersonDO.getId());
    }

}
